#pragma warning disable 108
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Cephei;
using Cephei.Core;
using Cephei.Core.Generic;
using Microsoft.FSharp.Core;
using Cephei.QL;
namespace Cephei.QL.Experimental.Credit
{
    /// <summary> 
	/// ! Reference: John Hull and Alan White, The Perfect Copula, June 2006  Let \f$Q_i(t)\f$ be the cumulative probability of default of counterparty i before time t.  In a one-factor model, consider random variables \f[ Y_i = a_i\,M+\sqrt{1-a_i^2}\:Z_i \f] where \f$M\f$ and \f$Z_i\f$ have independent zero-mean unit-variance distributions and \f$-1\leq a_i \leq 1\f$.  The correlation between \f$Y_i\f$ and \f$Y_j\f$ is then \f$a_i a_j\f$.  Let \f$F_Y(y)\f$ be the cumulative distribution function of \f$Y_i\f$. \f$y\f$ is mapped to \f$t\f$ such that percentiles match, i.e. \f$F_Y(y)=Q_i(t)\f$ or \f$y=F_Y^{-1}(Q_i(t))\f$.  Now let \f$F_Z(z)\f$ be the cumulated distribution function of \f$Z_i\f$.  For given realization of \f$M\f$, this determines the distribution of \f$y\f$: \f[ Prob \,(Y_i &lt; y|M) = F_Z \left( \frac{y-a_i\,M}{\sqrt{1-a_i^2}}\right) \qquad \mbox{or} \qquad Prob \,(t_i &lt; t|M) = F_Z \left( \frac{F_Y^{-1}(Q_i(t))-a_i\,M} {\sqrt{1-a_i^2}} \right) \f]  The distribution functions of \f$ M, Z_i \f$ are specified in derived classes. The distribution function of \f$ Y \f$ is then given by the convolution \f[ F_Y(y) = Prob\,(Y&lt;y) = \int_{-\infty}^\infty\,\int_{-\infty}^{\infty}\: D_Z(z)\,D_M(m) \quad \Theta \left(y - a\,m - \sqrt{1-a^2}\,z\right)\,dm\,dz, \qquad \Theta (x) = \left\{ \begin{array}{ll} 1 &amp; x \geq 0 \\ 0 &amp; x &lt; 0 \end{array}\right. \f] where \f$ D_Z(z) \f$ and \f$ D_M(m) \f$ are the probability densities of \f$ Z\f$ and \f$ M, \f$ respectively.  This convolution can also be written \f[ F(y) = Prob \,(Y &lt; y) = \int_{-\infty}^\infty D_M(m)\,dm\: \int_{-\infty}^{g(y,a,m)} D_Z(z)\,dz, \qquad g(y,a,m) = \frac{y - a\cdot m}{\sqrt{1-a^2}}, \qquad a &lt; 1 \f]  or  \f[ F(y) = Prob \,(Y &lt; y) = \int_{-\infty}^\infty D_Z(z)\,dz\: \int_{-\infty}^{h(y,a,z)} D_M(m)\,dm, \qquad h(y,a,z) = \frac{y - \sqrt{1 - a^2}\cdot z}{a}, \qquad a &gt; 0. \f]  In general, \f$ F_Y(y) \f$ needs to be computed numerically.  \todo Improve on simple Euler integration
	/// </summary>
    [Guid ("80A083CB-883D-4c72-B0D9-5093AF1523AD"),ComVisible(true)]
	public interface IOneFactorCopula 
	{
		///////////////////////////////////////////////////////////////
        // Methods
        //
        /// <summary> 
		/// -------------------------------------------------------------------------
		/// </summary>
		 Int32 CheckMoments(Double tolerance);
        /// <summary> 
		/// 
		/// </summary>
		 Cephei.Core.IVector<Double> ConditionalProbability(Cephei.Core.IVector<Double> prob, Double m);
        /// <summary> 
		/// 
		/// </summary>
		 Double Correlation {get;}
        /// <summary> 
		/// -------------------------------------------------------------------------
		/// </summary>
		 Double CumulativeY(Double y);
        /// <summary> 
		/// 
		/// </summary>
		 Double CumulativeZ(Double z);
        /// <summary> 
		/// 
		/// </summary>
		 Double Density(Double m);
        /// <summary> 
		/// 
		/// </summary>
		 Double Integral(Double p);
        /// <summary> 
		/// -------------------------------------------------------------------------
		/// </summary>
		 Double InverseCumulativeY(Double p);
    }   

    /// <summary> 
	/// ! Reference: John Hull and Alan White, The Perfect Copula, June 2006  Let \f$Q_i(t)\f$ be the cumulative probability of default of counterparty i before time t.  In a one-factor model, consider random variables \f[ Y_i = a_i\,M+\sqrt{1-a_i^2}\:Z_i \f] where \f$M\f$ and \f$Z_i\f$ have independent zero-mean unit-variance distributions and \f$-1\leq a_i \leq 1\f$.  The correlation between \f$Y_i\f$ and \f$Y_j\f$ is then \f$a_i a_j\f$.  Let \f$F_Y(y)\f$ be the cumulative distribution function of \f$Y_i\f$. \f$y\f$ is mapped to \f$t\f$ such that percentiles match, i.e. \f$F_Y(y)=Q_i(t)\f$ or \f$y=F_Y^{-1}(Q_i(t))\f$.  Now let \f$F_Z(z)\f$ be the cumulated distribution function of \f$Z_i\f$.  For given realization of \f$M\f$, this determines the distribution of \f$y\f$: \f[ Prob \,(Y_i &lt; y|M) = F_Z \left( \frac{y-a_i\,M}{\sqrt{1-a_i^2}}\right) \qquad \mbox{or} \qquad Prob \,(t_i &lt; t|M) = F_Z \left( \frac{F_Y^{-1}(Q_i(t))-a_i\,M} {\sqrt{1-a_i^2}} \right) \f]  The distribution functions of \f$ M, Z_i \f$ are specified in derived classes. The distribution function of \f$ Y \f$ is then given by the convolution \f[ F_Y(y) = Prob\,(Y&lt;y) = \int_{-\infty}^\infty\,\int_{-\infty}^{\infty}\: D_Z(z)\,D_M(m) \quad \Theta \left(y - a\,m - \sqrt{1-a^2}\,z\right)\,dm\,dz, \qquad \Theta (x) = \left\{ \begin{array}{ll} 1 &amp; x \geq 0 \\ 0 &amp; x &lt; 0 \end{array}\right. \f] where \f$ D_Z(z) \f$ and \f$ D_M(m) \f$ are the probability densities of \f$ Z\f$ and \f$ M, \f$ respectively.  This convolution can also be written \f[ F(y) = Prob \,(Y &lt; y) = \int_{-\infty}^\infty D_M(m)\,dm\: \int_{-\infty}^{g(y,a,m)} D_Z(z)\,dz, \qquad g(y,a,m) = \frac{y - a\cdot m}{\sqrt{1-a^2}}, \qquad a &lt; 1 \f]  or  \f[ F(y) = Prob \,(Y &lt; y) = \int_{-\infty}^\infty D_Z(z)\,dz\: \int_{-\infty}^{h(y,a,z)} D_M(m)\,dm, \qquad h(y,a,z) = \frac{y - \sqrt{1 - a^2}\cdot z}{a}, \qquad a &gt; 0. \f]  In general, \f$ F_Y(y) \f$ needs to be computed numerically.  \todo Improve on simple Euler integration Factory
	/// </summary>
   	[ComVisible(true)]
    public interface IOneFactorCopula_Factory 
    {
        ///////////////////////////////////////////////////////////////
        // Factory methods
        //
    }
}

